package question03;


/**
 * @author yuan
 * @date 2021/11/28
 */
public class Solution {

    public static void main(String[] args) {
        // String test = "abcabcbb";
        // String test = "bbbbb";
        // String test = "pwwkew";
        // String test = " ";
        // String test = "";
        // String test = "au";
        String test = "dvdf";
        System.out.println(new Solution().lengthOfLongestSubstring(test));

    }

    public int lengthOfLongestSubstring(String s) {

        if (s.length() == 0 || s.length() == 1) return s.length();
        StringBuilder stringBuilder = new StringBuilder();
        int index = 0;
        int maxSize = 0;
        while (index < s.length()) {
            // 如果字符在字符串中未重复
            if (stringBuilder.indexOf(s.charAt(index) + "") == -1) {
                stringBuilder.append(s.charAt(index));
                index++;
            }else {
                // 重复
                // 对长度进行重新赋值
                maxSize = Math.max(stringBuilder.length(), maxSize);
                // 重新构造 StringBuilder
                stringBuilder.deleteCharAt(0);
            }
        }
        maxSize = Math.max(stringBuilder.length(), maxSize);
        return maxSize;
    }
}
